package com.niit.shopsys;

// ===== 订单服务（被测试对象）=====
public class OrderService {
    private InventorySystem inventory;
    private PricingSystem pricing;

    public OrderService(InventorySystem inventory, PricingSystem pricing) {
        this.inventory = inventory;
        this.pricing = pricing;
    }

    public OrderCreationResult createOrder(String productId, int quantity,
                                           String customerTier) {
        // 1. 检查库存
        boolean stockAvailable = inventory.checkStock(productId, quantity);
        if (!stockAvailable) {
            return new OrderCreationResult(false, null, 0, "Out of stock");
        }

        // 2. 获取价格
        double unitPrice = pricing.getPrice(productId, customerTier);
        double totalPrice = unitPrice * quantity;

        // 3. 预留库存
        String orderId = "ORD_" + productId + "_" + quantity;
        boolean reservation = inventory.reserveStock(productId, quantity, orderId);

        if (!reservation) {
            return new OrderCreationResult(false, null, 0, "Reservation failed");
        }

        // 4. 创建订单
        return new OrderCreationResult(true, orderId, totalPrice, null);
    }
}
